<?php

namespace app\exception\handler;

use app\customer\BizResponse;
use app\consts\Code;
use app\exception\BizException;
use Throwable;
use Webman\Exception\ExceptionHandler;
use Webman\Http\Request;
use Webman\Http\Response;

class AdminExceptionHandler extends ExceptionHandler
{
    use BizResponse;

    public $dontReport = [
        BizException::class,//业务逻辑异常不记录日志
    ];


    public function report(Throwable $exception)
    {
        if ($this->shouldntReport($exception)) {
            return;
        }
        $logs = '';
        if ($request = \request()) {
            $logs = $request->getRealIp() . ' ' . $request->method() . ' ' . trim($request->fullUrl(), '/');
        }
        $this->logger->error($logs . ' ' . $exception->getMessage());
    }


    public function render(Request $request, Throwable $exception): Response
    {
        if ($exception instanceof BizException){
            return $this->error($exception->getCode());
        }
        if ($exception instanceof \PDOException){
            return $this->error(Code::ERROR_DATABASE);
        }
        return $this->error(Code::ERROR);
    }
}